//
//  CTVSync.h
//  Channltv
//
//  Created by Bruno Keymolen on 7/5/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "CTVCache.h"
#import "CTVConfig.h"

/* Organistation of the cache:
 * Data:       ~/channl.tv/Data			> xml files from channl.tv (channels & clipsinchannel)
 * Media:      ~/channl.tv/Movies		> movies (clips) downloaded from the internet (have an extension)
 * Resources:  ~/channl.tv/Resources	> thumbnails etc
 * 
 * Cahche entrys are represented as an MD5 HASH of the URL pointing to the original content
 */

typedef enum CTVSyncState{
	CTVSyncState_NotReady,
	CTVSyncState_Idle,
	CTVSyncState_ChannelCollection,
	CTVSyncState_ChannelDetail
}CTVSyncState;


@class CTVSync, CTVSyncListener;


@protocol CTVSyncListener
	-(void)syncStarted;
	-(void)syncStopped;
	-(void)syncWillFinish;  //Called after the sync process but before existing clips/channels are deleted from mem
@end

@interface CTVSync : NSObject <CTVCacheListener> {
	CTVSyncState m_syncState;
	CTVConfig* m_config;
	NSMutableArray* m_syncData;
	NSMutableArray* m_syncMedia;
	NSMutableArray* m_syncResources;
	NSMutableDictionary* m_inSyncURLs;
	NSMutableArray* m_listeners;
	int m_syncLastError;

}

+(id)GetInstance;
-(void)setup;
-(int)syncWithNetwork;
-(BOOL)inSync;
-(void)addListener:(CTVSyncListener*)listener;
-(void)removeListener:(CTVSyncListener*)listener;

-(int)syncChannelsFromDisk;
-(int)syncLastError;


//CTVCacheListener
-(void)cacheFinishedForURL:(NSString*)url andStatusCode:(int)statusCode;

@end


@interface CTVSync (Private)
	//Private functions
	-(void)_gotoState:(CTVSyncState)state;
	-(void)_parseAndHandleChannel:(NSString*)url;
	-(void)_parseAndHandleClipsInChannel:(NSString*)url;
	-(void)_syncDiskFiles;
@end